package com.wlcj.www.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.IocBean;

import com.wlcj.www.model.platform.EvalReply;
import com.wlcj.www.model.platform.PlatformEval;
import com.wlcj.www.model.platform.PlatformEvalExt;

@IocBean
public class PlatEvalService {

	/**
	 * 平台总评价列表
	 * @return
	 */
	public List<PlatformEvalExt> getPlatformEvalList(int pid){
		Map<String,Object> map = new HashMap<String, Object>(); 
		List<PlatformEval> list = PlatformEval.dao.getEvalList(pid);//平台评论
		List<EvalReply> reply = new ArrayList<EvalReply>(); //评论回复
		if(CollectionUtils.isNotEmpty(list)){
			for(int i=0;i<list.size();i++){
				List<EvalReply> er = EvalReply.dao.getEvalReplyList(list.get(i).getInt("platform_evaluation_id"));
				reply.addAll(er);
			}
		}
		map.put("ev", list);
		map.put("rp", reply);
		return null;
	}
	
	/**
	 * 平台详情 评价-总评价
	 * @return
	 */
	public PlatformEvalExt getEvalByPlatId(int pid){
		return PlatformEvalExt.dao.getEval(pid);
	}
	
	/**
	 * 把平台评价表中的数据求平均数
	 * @return
	 */
	public PlatformEval getAvgPlatEval(int pid){
		return PlatformEval.dao.getAvgPlatEval(pid);
	}
	
	/**
	 * 添加评论
	 * @param entity
	 */
	@Aop("tx")
	public void addEval(Map<String,Object> entity){
		PlatformEval pf = new PlatformEval();
		pf.setAttrs(entity);
		pf.save();
		int pid = (Integer)entity.get("platform_id");
		// 操作成功后要添加扩展表内容
		PlatformEvalExt pe = getEvalByPlatId(pid);
		// 把平台评价表中的数据求平均数
		PlatformEval pfeval = getAvgPlatEval(pid);
		PlatformEvalExt pext = new PlatformEvalExt();//平台扩展表
		if(pe!=null){
			Map<String,Object> ppmap = new HashMap<String, Object>();
			//修改
			Long total  = pfeval.getLong("total");//总数
			//用户推荐度  = 推荐人数/总数
			int good = pe.getInt("good_number");//推荐人数
			ppmap.put("ext_id", pe.getInt("ext_id"));//主键id
			ppmap.put("recommend", good/total);//用户推荐度
			int coment = (Integer)entity.get("user_coment");//1推荐 2一般 3不推荐
			if(coment==1){//用户推荐度
				ppmap.put("good_number", pe.getInt("good_number")+1);
			}else if (coment==2){
				ppmap.put("common_number", pe.getInt("common_number")+1);
			}else{
				ppmap.put("bad_number", pe.getInt("bad_number")+1);
			}
			ppmap.put("totle", total);
			ppmap.put("customer_service", pfeval.getBigDecimal("customer_service"));//客户服务
			ppmap.put("money_on_guard", pfeval.getBigDecimal("money_on_guard"));//资金站岗
			ppmap.put("site_experience", pfeval.getBigDecimal("site_experience"));//网站体验
			pext.setAttrs(ppmap); 
			pext.update();
		}else{
			//新增
			Map<String,Object> pamap = new HashMap<String, Object>();
			pamap.put("platform_id", pid);
			int coment = (Integer)entity.get("user_coment");//1推荐 2一般 3不推荐
			if(coment==1){//用户推荐度
				pamap.put("recommend", 100);
				pamap.put("good_number", 1);
			}else if (coment==2){
				pamap.put("recommend", 0);
				pamap.put("common_number", 1);
			}else{
				pamap.put("recommend", 0);
				pamap.put("bad_number", 1);
			}
			pamap.put("totle", 1);
			pamap.put("customer_service", entity.get("customer_service"));
			pamap.put("money_on_guard", entity.get("money_on_guard"));
			pamap.put("site_experience", entity.get("site_experience"));
			pamap.put("create_user", pid);
			pamap.put("create_time", new Date());
			pext.setAttrs(pamap);
			pext.save();
		}
	}
	/**
	 * 添加回复
	 * @param entity
	 */
	@Aop("tx")
	public void addReply(Map<String,Object> entity){
		EvalReply er = new EvalReply();
		er.setAttrs(entity);
		er.save();
	}
}
